package com.ruoyi.hxf.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.hxf.domain.Type;
import com.ruoyi.hxf.domain.vo.TypeVo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.hxf.domain.bo.SubjectBo;
import com.ruoyi.hxf.domain.vo.SubjectVo;
import com.ruoyi.hxf.domain.Subject;
import com.ruoyi.hxf.mapper.SubjectMapper;
import com.ruoyi.hxf.service.ISubjectService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 课程的管理Service业务层处理
 *
 * @author hxf
 * @date 2022-12-16
 */
@RequiredArgsConstructor
@Service
public class SubjectServiceImpl implements ISubjectService {

    private final SubjectMapper baseMapper;

    /**
     * 查询课程的管理
     */
    @Override
    public SubjectVo queryById(Long subjectId) {
        return baseMapper.selectVoById(subjectId);
    }

    /**
     * 查询课程的管理列表
     */
    @Override
    public TableDataInfo<SubjectVo> queryPageList(SubjectBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<Subject> lqw = buildQueryWrapper(bo).last("order by create_time desc");
        Page<SubjectVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询课程的管理列表
     */
    @Override
    public List<SubjectVo> queryList(SubjectBo bo) {
        LambdaQueryWrapper<Subject> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<Subject> buildQueryWrapper(SubjectBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<Subject> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getSubjectName()), Subject::getSubjectName, bo.getSubjectName());
        lqw.between(params.get("beginCreateTime") != null && params.get("endCreateTime") != null,
            Subject::getCreateTime, params.get("beginCreateTime"), params.get("endCreateTime"));
        lqw.eq(Subject::getIsDelete, 0);
        return lqw;
    }

    /**
     * 新增课程的管理
     */
    @Override
    public Boolean insertByBo(SubjectBo bo) {
        Subject add = BeanUtil.toBean(bo, Subject.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setSubjectId(add.getSubjectId());
        }
        return flag;
    }

    /**
     * 修改课程的管理
     */
    @Override
    public Boolean updateByBo(SubjectBo bo) {
        Subject update = BeanUtil.toBean(bo, Subject.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(Subject entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除课程的管理
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        for (Long id : ids) {
            SubjectVo subjectVo = baseMapper.selectVoById(id);
            subjectVo.setIsDelete("1");
            Subject update = BeanUtil.toBean(subjectVo, Subject.class);
            baseMapper.updateById(update);
        }
        return true;
    }

    @Override
    public int myUpdateSubject(SubjectBo bo) {
        return baseMapper.myUpdateSubject(bo);
    }
}
